//
//  JobDetailSupportView.swift
//  GreenDeal
//
//  Created by Moennig Q on 27/12/2022.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
enum SupportType: String {
    case phone = "Phone"
    case email = "Email"
}
class JobDetailSupportView: CustomBackView {
    fileprivate lazy var disposeBag = DisposeBag()
    var dataObserver: BehaviorRelay<[SupportType]> = BehaviorRelay(value: [])
    let tableView = UITableView()
    //
    var tableViewHeightConstraint: Constraint?
    override func setUpView() {
        
        self.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(-16)
            self.tableViewHeightConstraint = make.height.greaterThanOrEqualTo(0).constraint
            make.bottom.equalTo(-16)
        }
        
        tableView.separatorStyle = .none
        tableView.isScrollEnabled = false
        tableView.register(JobDetailSupportTableViewCell.self, forCellReuseIdentifier: JobDetailSupportTableViewCell.reuseCellIdentifier)
        
        dataObserver.bind(to: tableView.rx.items(cellIdentifier: JobDetailSupportTableViewCell.reuseCellIdentifier,cellType: JobDetailSupportTableViewCell.self)){(row, type, cell) in
            cell.setData(type)
        }.disposed(by: self.disposeBag)
        //监听属性变化
        tableView.rx.observe(CGSize.self,"contentSize").subscribe {[weak self] (size) in
            guard let contentSize = size else {return}
            self?.tableViewHeightConstraint?.update(offset: contentSize.height)
        }.disposed(by: self.disposeBag)
        //
        self.setData()
    }
    private func setData(){
        var list = [SupportType]()
        list.append(.phone)
        list.append(.email)
        dataObserver.accept(list)
    }
}
